package com.zxl.always.on.display.track;

import android.app.Activity;
import android.content.Context;

import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.zxl.always.on.display.ActivityLifecycle;
import com.zxl.always.on.display.ApplicationHelper;
import com.zxl.always.on.display.configs.ChannelHelper;
import com.zxl.always.on.display.configs.sdk.OnlineConfigAgent;
import com.zxl.always.on.display.tools.LogUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/26.
 */
public class MobclickAgentHelper {

    public static void init(Context context) {
        try {
            UMConfigure.init(context, UMConfigure.DEVICE_TYPE_PHONE, null);
            OnlineConfigAgent.getInstance().init("5abd9fa0f29d981f3500009b", ChannelHelper.getChannel());
            UMConfigure.setLogEnabled(ApplicationHelper.DEBUG);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void onResume(Context context) {
        try {
            MobclickAgent.onResume(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void onPause(Context context) {
        try {
            MobclickAgent.onPause(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void onKillProcess(Context context) {
        try {
            MobclickAgent.onKillProcess(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void reportError(Context context, String error) {
        try {
            MobclickAgent.reportError(context, error);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void onEvent(Context context, String type, String event) {
        try {
            Activity activity = ActivityLifecycle.getActivity(null);
            if (activity != null && !Activity.class.isInstance(context)) {
                context = activity;
            }
            MobclickAgent.onEvent(context, type, event);
            LogUtils.d("onEvent : " + type + ", " + event);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public static void onEvent(Context context, StatisticsType type, String key, String value) {
        onEvent(context, type.getName(), key, value);
    }

    public static void onEvent(Context context, String type, String key, String value) {
        Map<String, String> map = new HashMap<>();
        map.put(key, value);
        MobclickAgentHelper.onEvent(context, type, map);
    }

    public static void onEvent(Context context, String type, Map<String, String> event) {
        try {
            Activity activity = ActivityLifecycle.getActivity(null);
            if (activity != null && !Activity.class.isInstance(context)) {
                context = activity;
            }
            MobclickAgent.onEvent(context, type, event);
            LogUtils.d("onEvent : " + type + ", " + event);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

}
